package com.sagittarius.demo.controller;

import com.sagittarius.demo.model.Person;
import com.sagittarius.demo.util.ExcelUtils;
import com.sagittarius.demo.util.ExportExcelParam;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * @ClassName TestController.java
 * @Author 李瑞珂 [18375669585@163.com]
 * @Version 1.0.0
 * @Description 演示使用工具类之后的导出
 * @CreateTime 2022年04月28日 16:42:00
 */
@RestController
@RequestMapping("test/")
public class TestController {

    /**
     * 演示导出单个sheetExcel
     */
    @GetMapping("/export/excel")
    public void exportExcel(HttpServletResponse response) {
        List<List<String>> headList = Collections.singletonList(Arrays.asList("编号", "名称", "性别"));
        //rotateHead这里是为了旋转表头便于开发和理解
        List<List<String>> lists = ExcelUtils.rotateHead(headList);
        ExportExcelParam exportExcelParam = ExportExcelParam.builder().excelName("123").
                headList(lists).data(getAllPerson()).build();
        ExcelUtils.writeExcel(response, exportExcelParam);
    }

    /**
     * 演示导出单个Excel模板
     * 在开发中会提供一个下载模板的接口来进行导入
     */
    @GetMapping("/export/excel/template")
    public void exportExcelTemplate(HttpServletResponse response) {
        List<List<String>> headList = Collections.singletonList(Arrays.asList("编号", "名称", "性别"));
        //rotateHead这里是为了旋转表头便于开发和理解
        List<List<String>> lists = ExcelUtils.rotateHead(headList);
        ExportExcelParam exportExcelParam = ExportExcelParam.builder().excelName("123").
                headList(lists).data(getAllPerson()).build();
        ExcelUtils.writeExcel(response, exportExcelParam);
    }

    /**
     * 演示导出单个sheetExcel注解版
     * 实体类的注解就不写了自行百度一下很简单
     */
    @GetMapping("/export/excel/note")
    public void exportExcelNote(HttpServletResponse response) {
        ExportExcelParam exportExcelParam = ExportExcelParam.builder().excelName("123").
                openNote(true).data(getAllPerson()).build();
        ExcelUtils.writeExcel(response, exportExcelParam);
    }

    /**
     * 演示导出单个sheetExcel注解版
     * 实体类的注解就不写了自行百度一下很简单
     */
    @GetMapping("/export/excel/template/note")
    public void exportExcelNoteTemplate(HttpServletResponse response) {
        ExportExcelParam exportExcelParam = ExportExcelParam.builder().excelName("123").
                //这个单括号很重要,因为注解版导出模板需要取出空集合里面泛型的Class
                openNote(true).data(new ArrayList<Person>(){}).build();
        ExcelUtils.writeExcel(response, exportExcelParam);
    }

    /**
     * 演示导出多个sheet空间的Excel
     */
    @GetMapping("/export/excel/great/sheet")
    public void exportExcelGreatSheet(HttpServletResponse response) {
        List<List<String>> headList = Collections.singletonList(Arrays.asList("编号", "名称", "性别"));
        //rotateHead这里是为了旋转表头便于开发和理解
        List<List<String>> lists = ExcelUtils.rotateHead(headList);
        List<List<String>> lists1 = ExcelUtils.rotateHead(headList);
        List<List<String>> lists2 = ExcelUtils.rotateHead(headList);
        Set<String> strings3 = new TreeSet<>(Arrays.asList("Sheet", "Sheet1", "Sheet2"));
        ExportExcelParam exportExcelParam = ExportExcelParam.builder().excelName("123").
                sheetNameSetGreatSheet(strings3).openNote(true).
                headListGreatSheet(Arrays.asList(lists, lists1, lists2)).
                dataList(Arrays.asList(getAllPerson(),getAllPerson(),getAllPerson())).build();
        ExcelUtils.writeExcelGreatSheet(response, exportExcelParam);
    }

    /**
     * 演示导出多个sheet空间的Excel模板
     */
    @GetMapping("/export/excel/great/sheet/template")
    public void exportExcelGreatSheetTemplate(HttpServletResponse response) {
        List<List<String>> headList = Collections.singletonList(Arrays.asList("编号", "名称", "性别"));
        //rotateHead这里是为了旋转表头便于开发和理解
        List<List<String>> lists = ExcelUtils.rotateHead(headList);
        List<List<String>> lists1 = ExcelUtils.rotateHead(headList);
        List<List<String>> lists2 = ExcelUtils.rotateHead(headList);
        Set<String> strings3 = new TreeSet<>(Arrays.asList("Sheet", "Sheet1", "Sheet2"));
        ExportExcelParam exportExcelParam = ExportExcelParam.builder().excelName("123").
                sheetNameSetGreatSheet(strings3).
                headListGreatSheet(Arrays.asList(lists, lists1, lists2)).
                dataList(Arrays.asList(new ArrayList<Person>(){},new ArrayList<Person>(){},new ArrayList<Person>(){})).build();
        ExcelUtils.writeExcelGreatSheet(response, exportExcelParam);
    }

    /**
     * 演示导出多个sheet空间的Excel注解版
     * 实体类的注解就不写了自行百度一下很简单
     */
    @GetMapping("/export/excel/great/sheet/template/note")
    public void exportExcelGreatSheetTemplateNote(HttpServletResponse response) {
        Set<String> strings3 = new TreeSet<>(Arrays.asList("Sheet", "Sheet1", "Sheet2"));
        ExportExcelParam exportExcelParam = ExportExcelParam.builder().excelName("123").
                sheetNameSetGreatSheet(strings3).openNote(true).
                dataList(Arrays.asList(new ArrayList<Person>(){},new ArrayList<Person>(){},new ArrayList<Person>(){})).build();
        ExcelUtils.writeExcelGreatSheet(response, exportExcelParam);
    }

    /**
     * 只是演示导出所以不用数据库
     * @return List<Person>
     */
    public static List<Person> getAllPerson() {
        Person person = new Person(1, "李四", "男");
        Person person1 = new Person(2, "王五", "男");
        Person person2 = new Person(3, "张三", "男");
        Person person3 = new Person(4, "小明", "男");
        Person person4 = new Person(5, "小红", "男");
        Person person5 = new Person(6, "小黄", "男");
        List<Person> list = new ArrayList<>();
        list.add(person);
        list.add(person1);
        list.add(person2);
        list.add(person3);
        list.add(person4);
        list.add(person5);
        return list;
    }

}
